let currentNumber = "";
let previousNumber = "";
let operator = "";

function appendToDisplay(num) {
  currentNumber += num;
  document.getElementById("display").value = currentNumber;
}

function calculate(op) {
  if (currentNumber) {
    previousNumber = currentNumber;
    operator = op;
    currentNumber = "";
    document.getElementById("display").value = "";
  }
}

function calculateResult() {
  let result;
  switch (operator) {
    case "+":
      result = parseFloat(previousNumber) + parseFloat(currentNumber);
      break;
    case "-":
      result = parseFloat(previousNumber) - parseFloat(currentNumber);
      break;
    case "X":
      result = parseFloat(previousNumber) * parseFloat(currentNumber);
      break;
    case "/":
      if (currentNumber !== "0") {
        result = parseFloat(previousNumber) / parseFloat(currentNumber);
      } else {
        alert("除数不能为零！");
        return;
      }
      break;
    case "%":
      if (currentNumber !== "0") {
        result = parseFloat(previousNumber) % parseFloat(currentNumber);
      } else {
        alert("余数不能为零！");
        return;
      }
      break;
  }
  document.getElementById("display").value = result;
  previousNumber = result.toString();
  operator = "";
  if (result == undefined || result === 0 || result == Infinity) {
    currentNumber = "";
  } else {
    currentNumber = result.toString();
  }
}

function clearDisplay() {
  document.getElementById("display").value = "";
  previousNumber = "";
  currentNumber = "";
  operator = "";
}
function backspace() {
  if (document.getElementById("display").value.length > 0) {
    let currentValue = document.getElementById("display").value;
    currentValue = currentValue.slice(0, -1);
    document.getElementById("display").value = currentValue;

    if (currentValue === "") {
      previousNumber = "";
      currentNumber = "";
      operator = "";
    } else {
      currentNumber = currentValue;
    }
  }
}

document.addEventListener("keydown", function (event) {
  keyusu(event);
});
function keyusu(event) {
  if (event.target.tagName !== "INPUT" && event.target.id !== "display") {
    let key = event.key;
    if (event.key === "Escape" || event.key === "Esc" || event.keyCode === 27) {
    }
    if (!isNaN(key) && key >= "0" && key <= "9") {
      appendToDisplay(key);
    } else if (["+", "-", "*", "/"].includes(key)) {
      calculate(key);
    } else if (key === "c" || key === "C" || key == "Escape") {
      clearDisplay();
    } else if (key === "=" || key === "Enter") {
      calculateResult();
    } else if (key === "Backspace") {
      backspace();
    }
    event.preventDefault();
  }
}

window.addEventListener(
  "message",
  function (event) {
    // if (event.origin !== "/index.html") {
    //   return;
    // }

    var data = event.data;

    if (data.type === "keyEvent") {
      console.log("Received key event:", data.key);

      handleKeyEvent(data.key);
    }
  },
  false
);

function handleKeyEvent(key) {
  if (!isNaN(key) && key >= "0" && key <= "9") {
    appendToDisplay(key);
  } else if (["+", "-", "*", "/"].includes(key)) {
    calculate(key);
  } else if (key === "c" || key === "C" || key == "Escape") {
    clearDisplay();
  } else if (key === "=" || key === "Enter") {
    calculateResult();
  } else if (key === "Backspace") {
    backspace();
  }
}
